package panda.query.scan.index;

import panda.index.Index;
import panda.query.scan.Scanner;
import panda.query.scan.TableScanner;
import panda.query.struct.Attribute;
import panda.query.struct.Constant;

/**
 * A scanner that utilize the index to perform the
 * <i>SELECT</i> operation
 * @author Tian Yuan
 *
 */
public class IndexSelectScanner implements Scanner {
	private Index idx;
	private Constant val;
	private TableScanner ts;
	private int op;
	
	public IndexSelectScanner(Index idx, Constant val, TableScanner ts, int op) {
		this.idx = idx;
		this.val = val;
		this.ts = ts;
		this.op = op;
		init();
	}
	
	public void init() {
		idx.findSpecial(val, op);
	}
	
	public boolean next() {
		if (!idx.getNext()) return false;
		ts.setCursor(idx.getTupleToken());
		return true;
	}

	public void close() {
		idx.close();
		ts.close();
	}

	public Constant getValue(Attribute attr) {
		return ts.getValue(attr);
	}

	public boolean hasAttribute(Attribute name) {
		return ts.hasAttribute(name);
	}
	
}
